<script>
    var __AddUrl   = '{{ .CommonIn.GetUrlByKey "admin/role/add" }}';
    var __EditUrl   = '{{ .CommonIn.GetUrlByKey "admin/role/edit" }}?resId=';
    var __GrantUrl   = '{{ .CommonIn.GetUrlByKey "admin/role/grant" }}?resId=';
    var __DelUrl    = '{{ .CommonIn.GetUrlByKey "admin/role/doDel" }}';
    var __ListUrl  = '{{ .CommonIn.GetUrlByKey "admin/role/doList" }}';
    function delData (resId){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: __DelUrl,
            data: {resId:resId},
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        window.location.reload()
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            }
        });
    }
    function listRender(handler){

        handler.render({
            elem: '#currentTableId',
            url: __ListUrl,
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'title', width: 160, title: '角色名称'},
                {field: 'codeName', width: 160, title: '角色编码'},
                {field: 'orderNum', width: 80, title: '序号', sort: true},
                {field: 'bak', minWidth:150 , title: '备注'},
                {field: 'status', width: 80, title: '状态', templet: function (d) {
                        if (d.status == 1) {
                            return '<span class="layui-badge layui-bg-blue">启用</span>';
                        }else if (d.status == 2) {
                            return '<span class="layui-badge layui-bg-blue">禁用</span>';
                        }
                    },
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done:function(res, curr, count){
                console.log(res)
            }
        });
    }
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        listRender(table)
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: __AddUrl,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: __EditUrl +data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'grant') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: __GrantUrl +data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm("确认删除该记录？",{
                    title:"操作确认",
                    yes:function(index,layero){
                        delData(data.id)
                        layer.close(index)
                    },
                    cancel:function(index){
                    }

                })
            }
        });

    });
</script>
